﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类WorkReport_OutBox。
    /// </summary>
    public class WorkReport_OutBox
    {
        public WorkReport_OutBox()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from WorkReport_OutBox where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.WorkReport_OutBox model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into WorkReport_OutBox(");
            strSql.Append("RecordId,SenderId,SenderName,SenderCode,SendTime,Subject,Content,SenderDepartId,SenderDepartName,SenderCompanyId,SenderCompanyName,SenderCityId,SenderCityName,ReceiverId,ReceiverName,isDeleted,ParentId,RootId)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@SenderId,@SenderName,@SenderCode,@SendTime,@Subject,@Content,@SenderDepartId,@SenderDepartName,@SenderCompanyId,@SenderCompanyName,@SenderCityId,@SenderCityName,@ReceiverId,@ReceiverName,@isDeleted,@ParentId,@RootId)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "SenderId", DbType.Guid, model.SenderId);
            db.AddInParameter(dbCommand, "SenderName", DbType.AnsiString, model.SenderName);
            db.AddInParameter(dbCommand, "SenderCode", DbType.AnsiString, model.SenderCode);
            db.AddInParameter(dbCommand, "SendTime", DbType.DateTime, model.SendTime);
            db.AddInParameter(dbCommand, "Subject", DbType.AnsiString, model.Subject);
            db.AddInParameter(dbCommand, "Content", DbType.AnsiString, model.Content);
            db.AddInParameter(dbCommand, "SenderDepartId", DbType.Guid, model.SenderDepartId);
            db.AddInParameter(dbCommand, "SenderDepartName", DbType.AnsiString, model.SenderDepartName);
            db.AddInParameter(dbCommand, "SenderCompanyId", DbType.Guid, model.SenderCompanyId);
            db.AddInParameter(dbCommand, "SenderCompanyName", DbType.AnsiString, model.SenderCompanyName);
            db.AddInParameter(dbCommand, "SenderCityId", DbType.Guid, model.SenderCityId);
            db.AddInParameter(dbCommand, "SenderCityName", DbType.AnsiString, model.SenderCityName);
            db.AddInParameter(dbCommand, "ReceiverId", DbType.AnsiString, model.ReceiverId);
            db.AddInParameter(dbCommand, "ReceiverName", DbType.AnsiString, model.ReceiverName);
            db.AddInParameter(dbCommand, "isDeleted", DbType.Boolean, model.isDeleted);
            db.AddInParameter(dbCommand, "ParentId", DbType.Guid, model.ParentId);
            db.AddInParameter(dbCommand, "RootId", DbType.Guid, model.RootId);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.WorkReport_OutBox model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update WorkReport_OutBox set ");
            strSql.Append("SenderId=@SenderId,");
            strSql.Append("SenderName=@SenderName,");
            strSql.Append("SenderCode=@SenderCode,");
            strSql.Append("SendTime=@SendTime,");
            strSql.Append("Subject=@Subject,");
            strSql.Append("Content=@Content,");
            strSql.Append("SenderDepartId=@SenderDepartId,");
            strSql.Append("SenderDepartName=@SenderDepartName,");
            strSql.Append("SenderCompanyId=@SenderCompanyId,");
            strSql.Append("SenderCompanyName=@SenderCompanyName,");
            strSql.Append("SenderCityId=@SenderCityId,");
            strSql.Append("SenderCityName=@SenderCityName,");
            strSql.Append("ReceiverId=@ReceiverId,");
            strSql.Append("ReceiverName=@ReceiverName,");
            strSql.Append("isDeleted=@isDeleted,");
            strSql.Append("ParentId=@ParentId,");
            strSql.Append("RootId=@RootId");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "SenderId", DbType.Guid, model.SenderId);
            db.AddInParameter(dbCommand, "SenderName", DbType.AnsiString, model.SenderName);
            db.AddInParameter(dbCommand, "SenderCode", DbType.AnsiString, model.SenderCode);
            db.AddInParameter(dbCommand, "SendTime", DbType.DateTime, model.SendTime);
            db.AddInParameter(dbCommand, "Subject", DbType.AnsiString, model.Subject);
            db.AddInParameter(dbCommand, "Content", DbType.AnsiString, model.Content);
            db.AddInParameter(dbCommand, "SenderDepartId", DbType.Guid, model.SenderDepartId);
            db.AddInParameter(dbCommand, "SenderDepartName", DbType.AnsiString, model.SenderDepartName);
            db.AddInParameter(dbCommand, "SenderCompanyId", DbType.Guid, model.SenderCompanyId);
            db.AddInParameter(dbCommand, "SenderCompanyName", DbType.AnsiString, model.SenderCompanyName);
            db.AddInParameter(dbCommand, "SenderCityId", DbType.Guid, model.SenderCityId);
            db.AddInParameter(dbCommand, "SenderCityName", DbType.AnsiString, model.SenderCityName);
            db.AddInParameter(dbCommand, "ReceiverId", DbType.AnsiString, model.ReceiverId);
            db.AddInParameter(dbCommand, "ReceiverName", DbType.AnsiString, model.ReceiverName);
            db.AddInParameter(dbCommand, "isDeleted", DbType.Boolean, model.isDeleted);
            db.AddInParameter(dbCommand, "ParentId", DbType.Guid, model.ParentId);
            db.AddInParameter(dbCommand, "RootId", DbType.Guid, model.RootId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from WorkReport_OutBox ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.WorkReport_OutBox GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,SenderId,SenderName,SenderCode,SendTime,Subject,Content,SenderDepartId,SenderDepartName,SenderCompanyId,SenderCompanyName,SenderCityId,SenderCityName,ReceiverId,ReceiverName,isDeleted,ParentId,RootId from WorkReport_OutBox ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Hr.Entity.WorkReport_OutBox model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,SenderId,SenderName,SenderCode,SendTime,Subject,Content,SenderDepartId,SenderDepartName,SenderCompanyId,SenderCompanyName,SenderCityId,SenderCityName,ReceiverId,ReceiverName,isDeleted,ParentId,RootId ");
            strSql.Append(" FROM WorkReport_OutBox ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize, int PageIndex, string OrderField, string TableName, string strWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "WorkReport_OutBox");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.WorkReport_OutBox> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,SenderId,SenderName,SenderCode,SendTime,Subject,Content,SenderDepartId,SenderDepartName,SenderCompanyId,SenderCompanyName,SenderCityId,SenderCityName,ReceiverId,ReceiverName,isDeleted,ParentId,RootId ");
            strSql.Append(" FROM WorkReport_OutBox ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.WorkReport_OutBox> list = new List<Chenxin.Lib.Hr.Entity.WorkReport_OutBox>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.WorkReport_OutBox ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.WorkReport_OutBox model = new Chenxin.Lib.Hr.Entity.WorkReport_OutBox();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["SenderId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SenderId = new Guid(ojb.ToString());
            }
            model.SenderName = dataReader["SenderName"].ToString();
            model.SenderCode = dataReader["SenderCode"].ToString();  
            ojb = dataReader["SendTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SendTime = (DateTime)ojb;
            }
            model.Subject = dataReader["Subject"].ToString();
            model.Content = dataReader["Content"].ToString();
            ojb = dataReader["SenderDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SenderDepartId = new Guid(ojb.ToString());
            }
            model.SenderDepartName = dataReader["SenderDepartName"].ToString();
            ojb = dataReader["SenderCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SenderCompanyId = new Guid(ojb.ToString());
            }
            model.SenderCompanyName = dataReader["SenderCompanyName"].ToString();
            ojb = dataReader["SenderCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SenderCityId = new Guid(ojb.ToString());
            }
            model.SenderCityName = dataReader["SenderCityName"].ToString();
            model.ReceiverId = dataReader["ReceiverId"].ToString();
            model.ReceiverName = dataReader["ReceiverName"].ToString();
            ojb = dataReader["isDeleted"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.isDeleted = (bool)ojb;
            }
            ojb = dataReader["ParentId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ParentId = new Guid(ojb.ToString());
            }
            ojb = dataReader["RootId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RootId = new Guid(ojb.ToString());
            }
            return model;
        }

        #endregion  成员方法
    }
}

